home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3817 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.6 KB

  1. Path: prairienet.org!wemccaug
  2. From: wemccaug@prairienet.org (Wendy E. McCaughrin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q]Assigning function pointer in C/C++.
  5. Date: 26 Jan 1996 08:14:34 GMT
  6. Organization: University of Illinois at Urbana
  7. Message-ID: <4ea2ha$ap2@vixen.cso.uiuc.edu>
  8. References: <4doc42$gsb@bmdhh222.bnr.ca> <DL3JJu.5nB.0.queen@torfree.net>
  9. Reply-To: wemccaug@prairienet.org (Wendy E. McCaughrin)
  10. NNTP-Posting-Host: firefly.prairienet.org
  11.  
  12.  
  13. In a previous article, ray.fallon@nt.com (Ray Fallon) says:
  14.  
  15. >bh332@freenet.toronto.on.ca (Karim Ladha) wrote:
  16. >>
  17. >>
  18. >>How is it possible to assign a declared variable in C++ a pointer to
  19. >>some function member? If you know of a solution, post. Greatly appreciated.
  20. >>
  21. >
  22. >I response to your question, assigment of pointers to member functions is
  23. >not possible for the following reason : 
  24. >
  25. >In 'C' you can only have one instance of a function at any one time. In 
  26. >C++ you can have multiple instances of a class (i.e. objects) and the
  27. >pointer to the function would not be able to know which object you are
  28. >referring to. In any case, it is possible to use other methods, and 
  29. >theoritically they are more sound methods. Use the principle of 
  30. >polymorphism. 
  31.  This is correct, but there is a simpler answer as well. Member functions
  32.  get invoked through their objects, and upon invocation the object's 'this'
  33.  ptr gets passed to the member function so it can access the object's data
  34.  members. With a ptr to a member function, you could then invoke it without
  35.  going through any object, leaving the 'this' ptr expected by this function
  36.  essentially undefined. (well, maybe it's not a simplet answer, after all.)
  37.  
  38.  -- Scott McC.
  39.  
  40.